package com.just.sbweb.controller;

import com.just.sbweb.model.MySocketMessage;
import com.just.sbweb.model.MySocketResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@Controller
public class WSController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @MessageMapping("/web/welcome")
    @SendTo("/topic/getResponse")
    public MySocketResponse response(MySocketMessage message) throws InterruptedException {
        Thread.sleep(3000);
        return new MySocketResponse("Welcome,"+message.getName());
    }
    @MessageMapping("/chat")
    public void handleChat(Principal principal,String msg){
        String userName=principal.getName();
        System.out.println(userName);
        String targetUser;
        switch (userName){
            case "admin":{
                targetUser="erGouZi";
            }
            break;
            case "erGouZi":{
                targetUser="admin";
            }
            break;
            default:{
                targetUser="admin";
            }
        }
        simpMessagingTemplate.convertAndSendToUser(targetUser,"/queue/notifications",userName+"-send:"+msg);
    }
}
